# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)


class NumMatrix:
    def __init__(self, matrix: list):
        self.matrix = matrix
        for i in range(len(self.matrix)):
            self.matrix[i] = [0] + self.matrix[i]
            for j in range(1, len(self.matrix[i])):
                self.matrix[i][j] = self.matrix[i][j-1] + self.matrix[i][j]

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        res = 0
        for i in range(row1, row2+1):
            res += self.matrix[i][col2+1] - self.matrix[i][col1]
        return res


class NumMatrix2:
    """还是加0的快"""
    def __init__(self, matrix: list):
        self.matrix = matrix
        for i in range(len(self.matrix)):
            for j in range(1, len(self.matrix[i])):
                self.matrix[i][j] = self.matrix[i][j-1] + self.matrix[i][j]

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        res = 0
        for i in range(row1, row2+1):
            res += self.matrix[i][col2] - (self.matrix[i][col1-1] if col1 > 0 else 0)
        return res


class NumMatrix3:
    def __init__(self, matrix: list):
        """这里在本地执行不报错,在网上执行就会报错"""
        try:
            self.matrix = [[0 for j in range(len(matrix[0]))]]
        except:
            self.matrix = [[]]
        self.matrix.extend(matrix)
        print(self.matrix)
        for i in range(len(self.matrix)):
            self.matrix[i].insert(0, 0)
        for i in range(1, len(self.matrix)):
            for j in range(1, len(self.matrix[i])):
                self.matrix[i][j] = self.matrix[i][j] + self.matrix[i][j-1] + self.matrix[i-1][j] - self.matrix[i-1][j-1]

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        return self.matrix[row2+1][col2+1] - self.matrix[row2+1][col1] - self.matrix[row1][col2+1] + self.matrix[row1][col1]


# a = NumMatrix3([
#   [3, 0, 1, 4, 2],
#   [5, 6, 3, 2, 1],
#   [1, 2, 0, 1, 5],
#   [4, 1, 0, 1, 7],
#   [1, 0, 3, 0, 5]
# ])
# print(a.sumRegion(2, 1, 4, 3))
# print(a.sumRegion(1, 1, 2, 2))
# print(a.sumRegion(1, 2, 2, 4))
# a = NumMatrix([[0]])
# print(a.sumRegion(0, 0, 0, 0))
a = NumMatrix([[]])
